<template>
  <div class="lottery-info-list views-container">
    <el-dialog :visible.sync="dialogTableVisible" custom-class="reset-pwd wlm-form-dialog dialog-table">
      <div class="wlm-form" v-if="dialogTableVisible">
        <div class="wlm-form-header wlm-dialog-fixheader">
          <span class="wlm-dialog-inlenr">中奖商品</span>
        </div>
        <el-scrollbar wrap-class="scrollbar-wrapper">
          <div class="wlm-form-content">
            <div class="wlm-dialog-main">
              <div class="wlm-dialog-content" v-loading="lotteryGoods">
                <el-form ref="userForm" size="small" label-width="120px" class="retail-form" label-position="right">
                  <template>
                    <el-table :data="lotteryGoodsData" style="width: 100%">
                      <el-table-column label="奖品名称">
                        <template slot-scope="scope">
                          <span>{{scope.row.prize_title}}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="领取商家">
                        <template slot-scope="scope">
                          <span>{{scope.row.bus_name}}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="领取门店">
                        <template slot-scope="scope">
                          <span>{{scope.row.store_name}}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="领取状态">
                        <template slot-scope="scope">
                          <span>{{scope.row.is_get == 1?'已领取':'未领取'}}</span>
                        </template>
                      </el-table-column>
                    </el-table>
                  </template>
                </el-form>
              </div>
            </div>
          </div>
        </el-scrollbar>
      </div>
      <footer class="flex-row  flex-justify-c flex-align-c wlm-dialog-fixfooter">
        <el-button size="small" type="primary" @click="dialogTableVisible = false">确定</el-button>
      </footer>
    </el-dialog>
    <div class="wlm-table">
      <div class="wlm-table-header">
        <el-form :model="tableFormatData.userTable.files" size="small" label-width="85px" class="retail-form" label-position="right">
          <el-form-item label="" class="filter">
            <el-form-item label="用户：">
              <el-input @keyup.enter.native="filesSerch" v-model="tableFormatData.userTable.files.nickName" placeholder="请输入用户昵称或ID" style="width:160px;"></el-input>
            </el-form-item>
            <el-form-item label="获奖状态：">
              <el-select v-model="tableFormatData.userTable.files.prize_state" placeholder="请选择">
                <el-option v-for="item in [{lable:'全部',value:''},{lable:'未开奖',value:'1'},{lable:'已获奖',value:'2'},{lable:'未获奖',value:'3'}]" :key="item.value" :label="item.lable" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="获奖码：">
              <el-input v-model="tableFormatData.userTable.files.luck_code" placeholder="请输入用户抽奖码" style="width:160px;"></el-input>
            </el-form-item>
          </el-form-item>
          <el-form-item label="" class="filter-btns">
            <el-button type="primary" @click="filesSerch">筛选</el-button>
            <el-button type="text" @click="filesEmpty">清空筛选条件</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="wlm-table-content">
        <!-- <p style="font-weight: 600;padding-bottom: 14px;">入账商品</p> -->
        <el-table :ref="tableFormatData.userTable.key" :data="tableFormatData.userTable.tableData" @selection-change="handleSelectionChange" style="width: 100%">
          <!-- <el-table-column type="selection" width="55">
                </el-table-column> -->
          <el-table-column prop="name" label="抽奖活动">
            <template slot-scope="scope">
              <span>{{scope.row.luck_title}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="用户">
            <template slot-scope="scope">
              <div class="wlm-table-logos flex-row flex-align-c flex-justify-c">
                <img class="logo" :src="scope.row.avatarUrl">
                <div class="inner-text">
                  <p style="white-space: nowrap;">{{scope.row.nickName}}</p>
                  <span>id:{{scope.row.user_id}}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="抽奖时间">
            <template slot-scope="scope">
              <span v-if="scope.row.luck_begin_time">{{scope.row.luck_begin_time*1000| parseTime('{y}-{m}-{d} {h}:{i}:{s}')}}</span>
              <span v-if="!scope.row.luck_begin_time">-</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="抽奖码">
            <template slot-scope="scope">
              <span>{{scope.row.luck_code}}</span>
            </template>
          </el-table-column>
           <el-table-column prop="name" label="表单信息" width="450px;">
               <template slot-scope="scope">
                    <div v-for="(item,index) in scope.row.form.pc_value" :key="index">
                      <!-- 单行文本、多行文本、身份证、时间、日期 -->
                        <div style="display:flex;">
                          <div>
                            {{keyValueTitle[item.key] ? keyValueTitle[item.key] :item.name}}:
                          </div>
                            <div v-if="['singletext','manytext','cardnumber','datetext','datatime'].indexOf(item.key) != -1">
                              {{item.default}}
                            </div>
                             <!-- 单选、多选、下拉框 -->
                          <div v-if="['downbox','checkbox','radio'].indexOf(item.key) != -1">
                            <div
                              v-for="(v,i) in item.meal"
                              :key="i"
                            >
                            {{item.default.split(',').indexOf(v.id) !=-1?v.balanceone:''}}
                             <!-- {{item.default.split()==v.id?v.balanceone:''}} -->
                              <!-- {{ v.balanceone }} -->
                            </div>
                          </div>
                          <!-- 图片 -->
                          <div v-if="['image'].indexOf(item.key) != -1">
                            <div style="display:flex;">
                              <div
                                v-for="(v,i) in item.default"
                                :key="i"
                              >
                                <img
                                  style="width:50px;height:50px;margin-right:10px;"
                                  :src="v?v:''"
                                >
                              </div>
                            </div>
                            </div>
                             <!-- 日期范围、时间范围 -->
                            <div v-if="['datatextrange','timeframe'].indexOf(item.key) != -1">
                              {{item.name.startname}}:{{item.default.startappointdata}} — {{item.name.endname}}:{{item.default.endappointdata}}

                            </div>
                             <!-- 城市 -->
                              <div v-if="['city'].indexOf(item.key) != -1">
                                {{item.address.province + '-' + item.address.city + '-' + item.address.region}}
                              </div>
                        </div>
                    </div>
                </template>
           </el-table-column>
          <el-table-column prop="name" label="获奖状态" min-width="40">
            <template slot-scope="scope">
              <span v-if="scope.row.prize_state==1">未开奖</span>
              <span v-if="scope.row.prize_state==2">已获奖</span>
              <span v-if="scope.row.prize_state==3">未获奖</span>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="name" label="获奖奖品" >
                    <template slot-scope="scope">
                        <span>{{scope.row.nickName}}</span>
                    </template>
                </el-table-column> -->
          <!-- <el-table-column label="获奖显示" >
                    <template slot-scope="scope">
                        <span :disabled="scope.row.prize_state==2" v-if='scope.row.is_no==1'>内定不中奖</span>
                        <span :disabled="scope.row.prize_state==2" v-if='scope.row.is_yes==1'>内定获奖</span>
                    </template>
                </el-table-column>  -->
          <el-table-column label="操作" width="300px;">
            <template slot-scope="scope">
              <el-button type="text" :disabled="scope.row.is_zf==2" v-if="scope.row.prize_state==2" @click="tovoidbtn(scope.row.luck_code_id)">作废</el-button>
              <el-button :disabled="scope.row.prize_state==2||scope.row.is_no==1" class="wlm-text" type="text" @click="scope.row.is_yes = scope.row.is_yes === 1 ? 2 : 1;changeTableItem({user_id:scope.row.user_id,luck_id:scope.row.luck_id,luck_code_id:scope.row.luck_code_id,is_yes:scope.row.is_yes},tableFormatData.userTable.change.is_yes,{isWarning: scope.row.is_yes === 1,warning:'是否确认内定获奖？',cancel:{msg:'已取消内定获奖',val:{scope:scope,cancelStatus:{key:'is_yes',val:2}}}})">{{scope.row.is_yes === 1 ? '已内定':'内定获奖'}}</el-button>
              <el-button :disabled='scope.row.prize_state==2||scope.row.is_yes==1' class="wlm-text" type="text" @click="scope.row.is_no = scope.row.is_no === 1 ? 2 : 1;changeTableItem({user_id:scope.row.user_id,luck_id:scope.row.luck_id,luck_code_id:scope.row.luck_code_id,is_no:scope.row.is_no},tableFormatData.userTable.change.is_no,{isWarning: scope.row.is_yes === 1,warning:'是否内定不中奖？',cancel:{msg:'已取消内定不中奖',val:{scope:scope,cancelStatus:{key:'is_no',val:2}}}})">{{scope.row.is_no === 1 ? '取消内定不中奖':'设置内定不中奖'}}</el-button>
              <el-button v-if="scope.row.prize_state==2" class="wlm-text" type="text" @click="showLotteryGoods(scope.row.luck_code_id)">查看奖品</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-content flex-row flex-justify-e flex-align-c">
          <!-- <el-row class="pagination-btns">
                    <el-checkbox class="check-all" true-label="1" false-label="0" v-model="tableFormatData.userTable.files.checkall" @change="toggleSelection">全部</el-checkbox>
                    <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button>
                </el-row> -->
          <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" :current-page.sync="tableFormatData.userTable.pagination.page" :page-sizes="tableFormatData.userTable.pagination.pagesizes" :page-size.sync="tableFormatData.userTable.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper" :total="hasTableData?tableFormatData.userTable.pagination.total : 0">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  luckUserList,
  luckHitDetail,
  isYes,
  isNO,
  luckHitZf
} from '@/api/application'
import mixins from '@/mixins/mixins'
export default {
  mixins: [mixins.getters('Table')],
  name: 'LotteryInfoList',
  components: {
  },
  created() {
    // getGroupsShow().then((response) => {
    //   const {
    //     data: {
    //       data: msgData = []
    //     }
    //   } = response
    //   this.categoryData = msgData
    // })
  },
  data() {
    return {
       keyValueTitle: {
        datatextrange: '日期范围',
        timeframe: '时间范围',
      },
      lotteryGoods: true,
      lotteryGoodsData: [],
      dialogTableVisible: false,
      categoryData: {},
      tableFormatData: {
        current: 'userTable',
        userTable: {
          key: 'userTable',
          api: {
            getList: luckUserList
          },
          tableData: [],
          files: {
            nickName: '',
            prize_state: '',
            luck_code: '',
            ids: [],
            // redirect: 'groups_id',
            checkall: '0',
            Recycle: '1'
          },
          change: {
            is_yes: {
              key: 'is_yes',
              api: isYes
            },
            is_no: {
              key: 'is_no',
              api: isNO
            }
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  methods: {
    showLotteryGoods(luck_code_id) {
      this.lotteryGoods = true
      this.dialogTableVisible = true
      luckHitDetail({ luck_code_id }).then((response) => {
        const { data: { data: lotteryGoodsData = [] } } = response
        this.lotteryGoodsData = lotteryGoodsData
        this.lotteryGoods = false
      })
    },
    tovoidbtn(luck_code_id){
        this.$confirm('继续操作将作废此中奖码（导致无法正常核销奖品），是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          luckHitZf({luck_code_id:luck_code_id}).then(res=>{
            if(res.data.code==1){
              this.$message.success("操作成功！")
                this.filesSerch()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }
  }
}
</script>
